<?php

class ChatController extends Controller
{
	public function actionIndex()
	{
		$criteria = new CDbCriteria();
  			$criteria->limit = 10;
  			$criteria->order ="id desc";
    		$dataProvider=Chat::model()->findAll($criteria);
    		$str ="";
    		foreach ($dataProvider as $key ) {

            $str .= '<div class="comt-one"><img src=  "'.
            Yii::app()->request->baseUrl.'/themes/bjoco/images/ava.gif" align="left" />'
            .$key->content;

            $str.='</div>';
    		}
            echo $str ;
	}

	public function actionChat()
	{
		$model = new Chat();
        if (Yii::app()->user->isGuest) {
            echo "<script>
                alert('Phiền bạn đăng nhập vào hệ thống!');
                </script>";
        }else {
            $model->user_id= Yii::app()->user->id;
            $model->content = $_POST['Chat']['content'];
            if ($model->save()){
                $criteria = new CDbCriteria();
                $criteria->limit = 10;
                $criteria->order ="id desc";
                $dataProvider=Chat::model()->findAll($criteria);
                $str ="";
                foreach ($dataProvider as $key ) {

                $str .= '<div class="comt-one"><img src=  "'.
                Yii::app()->request->baseUrl.'/themes/bjoco/images/ava.gif" align="left" />'
                .$key->content;

                $str.='</div>';
                }
                echo $str ;
            }
        }
	}
}
?>